<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Session;
use App\Services\ItemBankService;
use Illuminate\Http\Request;
use App\Services\TagService;

class ItemBankController extends Controller{
	public function qlist(Request $request){
		$username = Session::get('username');
		if(!$username){
			return redirect('/admin/login');
		}else{
			$data['username'] = $username;
			$data['page_size_list'] = array(20,25,50,100);
			$data['page_size'] = $request->input('page_size') ? intval($request->input('page_size')) : 15;
			$data['keywords'] = $request->input('keywords') ? addslashes($request->input('keywords')) : '';

			$ib = new ItemBankService;

			$data['questionlist'] = $ib->getQuestionList($data['page_size'],$data['keywords']);

			return view('admin.item_bank.qlist',$data);
		}
	}

	public function qcreate(){
		$username = Session::get('username');
		if(!$username){
			return redirect('/admin/login');
		}else{
			$tag = new TagService;
			$data['username'] = $username;
			$tagNum = $tag->getTagCount();
			$data['taglist'] = $tag->getTagList($tagNum);

			return view('admin.item_bank.qadd',$data);
		}
	}

	public function qstore(){
		date_default_timezone_set('PRC');
		$ib = new ItemBankService;
		$data['question'] = addslashes($_POST['question']);
		$data['tag_id'] = addslashes($_POST['tag_id']);
		$data['multi'] = intval($_POST['ismuti']);
		$data['add_date'] = date('Y-m-d H:i:s');
		$data['isshow'] = 1;

		$info = $ib->getQuestionInfoByName($data['question']);
		if(!empty($info)){
			$_data = array('code'=>300);
		}else{
			$res = $ib->insertQuestion($data);
			if($res){
				$_data = array('code'=>200);
			}else{
				$_data = array('code'=>400);
			}
		}

		return response()->json($_data);
	}

	public function qedit($id){
		$username = Session::get('username');
		if(!$username){
			return redirect('/admin/login');
		}else{
			$data['username'] = $username;

			$ib = new ItemBankService;
			$tag = new TagService;

			$data['info'] = $ib->getQuestionInfoById($id);
			$data['info']->tag_id = explode(',',$data['info']->tag_id);
			$tagNum = $tag->getTagCount();
			$data['taglist'] = $tag->getTagList($tagNum);


			return view('admin.item_bank.qedit',$data);
		}
	}

	public function qupdate(){
		date_default_timezone_set('PRC');
		$ib = new ItemBankService;

		$id = intval($_POST['id']);
        $data['question'] = addslashes($_POST['question']);
        $data['tag_id'] = addslashes($_POST['tag_id']);
        $data['multi'] = intval($_POST['ismuti']);
		$data['update_date'] = date('Y-m-d H:i:s');

		$res = $ib->modifyQuestion($id,$data);

		if($res){
			$_data = array('code'=>200);
		}else{
			$_data = array('code'=>400);
		}

		return response()->json($_data);
	}

	public function qdel($id){
		date_default_timezone_set('PRC');
		$ib = new ItemBankService;
		$data['isshow'] = 0;
		$data['update_date'] = date('Y-m-d H:i:s');

		$res = $ib->modifyQuestion($id,$data);
		if($res){
			$_data = array('code'=>200);
		}else{
			$_data = array('code'=>400);
		}
		return response()->json($_data);
	}

	public function a_list($id){
		$username = Session::get('username');

		if(!$username){
			return redirect('/admin/login');
		}else{
			$data['username'] = $username;
			$ib = new ItemBankService;
			$data['alist'] = $ib->getOptionsList($id);
			$data['id'] = $id;
			return view('admin.item_bank.anlist',$data);
		}
		


	}

	public function a_create($id){
		$username = Session::get('username');
		if(!$username){
			return redirect('/admin/login');
		}else{
			$ib = new ItemBankService;
			$data['username'] = $username;
			$data['qinfo'] = $ib->getQuestionInfoById($id);
			$data['question_id'] = $id;

			return view('admin.item_bank.anadd',$data);
		}
	}

	public function a_insert(){
		date_default_timezone_set('PRC');
		$ib = new ItemBankService;

		$data['option'] = addslashes($_POST['option']);
		$data['q_id'] = intval($_POST['q_id']);
		$data['add_date'] = date('Y-m-d H:i:s');
		$data['isshow'] = 1;

		$info = $ib->getOptionByName($data['option']);
		if(!empty($info)){
			$_data = array('code'=>300);
		}else{
			$res = $ib->insertOption($data);
			if($res){
				$_data = array('code'=>200);
			}else{
				$_data = array('code'=>400);
			}
		}

		return response()->json($_data);
	}

	public function a_edit($id){
		$username = Session::get('username');
		if(!$username){
			return redirect('/admin/login');
		}else{
			$ib = new ItemBankService;
			$qnum = $ib->getQuestionNum();
			$data['username'] = $username;
			$data['optioninfo'] = $ib->getOptionById($id);
			$data['question_list'] = $ib->getQuestionList($qnum);

			return view('admin.item_bank.aedit',$data);
		}
		

	}

	public function a_update(){
		date_default_timezone_set('PRC');
		$ib = new ItemBankService;

		$id = intval($_POST['id']);

		$data['option'] = addslashes($_POST['option']);
		$data['q_id'] = intval($_POST['q_id']);
		$data['update_date'] = date('Y-m-d H:i:s');
		$res = $ib->modifyOption($id,$data);
		if($res){
			$_data = array('code'=>200);
		}else{
			$_data = array('code'=>400);
		}

		return response()->json($_data);
	}

	public function a_del($id){
		date_default_timezone_set('PRC');
		$ib = new ItemBankService;
		$data['isshow'] = 0;
		$data['update_date'] = date('Y-m-d H:i:s');
		$res = $ib->modifyOption($id,$data);
		if($res){
			$_data = array('code'=>200);
		}else{
			$_data = array('code'=>400);
		}

		return response()->json($_data);
	}
}